package org.geoscrape.util;

/**
 * Utility class for parsing command line parameters.
 *
 */
public class ParamParser
{

	/**
	 * Get the value of the given parameter. If the parameter is present, but
	 * has no value, an empty string is returned. If the parameter is not
	 * present, null is returned.
	 * 
	 * @param param
	 * @param args
	 * @return
	 */
	public static String getParameter(String param, String[] args)
	{
		String res = null;
		for (int x = 0; x < args.length; x++)
		{
			if (args[x].startsWith("-"))
			{
				if (args[x].substring(1).equals(param))
				{
					// parameter found
					if (x >= args.length - 1 || args[x + 1].startsWith("-"))
					{
						// no value
						res = "";
					}
					else
					{
						// value is next argument
						res = args[x + 1];
					}
				}
			}
		}
		return res;
	}
	
	/**
	 * Check if a given parameter is present or not.
	 * @param param
	 * @param args
	 * @return true if the parameter/switch is given, false otherwise
	 */
	public static boolean isParameterPresent(String param,String[]args)
	{
		return getParameter(param,args)!=null;
	}
}
